﻿using System;
using System.Diagnostics;
using System.IO;

namespace OpcDaNet
{
    class Debugger
    {
        private static string getLogMethodName()
        {
            try
            {
                var frameIndex = 3;
                var st = new StackTrace(true);
                var sfs = st.GetFrames();
                var sf = sfs[frameIndex];
                var method = sf.GetMethod();
                var ln = sf.GetFileLineNumber();
                var methodName = $"{method.DeclaringType.FullName}>{method.Name}[{ln}]";
                return methodName;
            }
            catch { }
            return string.Empty;
        }

        public static void WriteLog(string log)
        {
            try
            {
                var appPath = new FileInfo(Process.GetCurrentProcess().MainModule.FileName);
                var methodName = getLogMethodName();
                var text = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}[{methodName}]>>{log}\r\n";
                var dir = new DirectoryInfo(Path.Combine(appPath.Directory.FullName, "DebugLog"));
                if (!dir.Exists)
                {
                    dir.Create();
                }
                var path = Path.Combine(dir.FullName, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
                File.AppendAllText(path, text);
            }
            catch
            { }
        }

    }
}
